/**
 * 用户
 * 我的问诊
 */
var uid, name, avatar, ws;
document.addEventListener('plusready',function(){ 
	ws = plus.webview.currentWebview();
	uid = plus.storage.getItem('uid');
	name = plus.storage.getItem('name');
	avatar = plus.storage.getItem('avatar');
	avatar = getAvatar(avatar);
	showWating();
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=chat&a=askList&uid="+uid,
		dataType :'json',
		success :function(data){
//			console.log(JSON.stringify(data));
			plus.nativeUI.closeWaiting();
			if(data){
				var list = '';
				$.each(data.video, function(k,v) {
//					console.log(JSON.stringify(v));
					list += '<div class="mui-table-view table-view-cell bbs-ico margin-bottom-5 testvideo" aid="'+v.aid+'" status="'+v.status+'" did="'+v.mem.aid+'">'+
								'<div class="overflow-h">'+
									'<i><img src="'+getAvatar(v.mem.avatar)+'"/></i>'+
									'<div>'+
										'<li>'+
											'<span>'+v.mem.name+'</span>'+
											'<span class="mui-h6 mui-pull-right">'+v.addtime+'</span>'+
										'</li>'+
										'<li class="mui-h6 ">'+ 
											'<span class="iconfont type-3">&#xe622;</span>'+
											(v.status == '9' || v.status == '1' ? '<span>' : '<span style="color:red;">');
								if(v.status == '0' || v.status == '8'){	//未接通、因其他原因造成未接通
									list += '未播通，点击重播';
								}else if(v.status == '1'){	//已接通
									list += '已接通';
								}else if(v.status == '2'){	//拒绝
									list += '已拒绝，点击重播';
								}else if(v.status == '9'){
									list += '视频已结束'+(v.totaltime ? ('，时长：'+v.totaltime) : '');
								}
								list += '</span></li>'+
									'</div>'+
								'</div>'+
							'</div>';
				});
				$.each(data.pictext, function(k,v) {
//					console.log(JSON.stringify(v));
					list += '<div class="mui-table-view table-view-cell bbs-ico margin-bottom-5 testPic" did="'+v.mem.aid+'">'+
								'<div class="overflow-h">'+
									'<i><img src="'+getAvatar(v.mem.avatar)+'"/></i>'+
									'<div>'+
										'<li>'+
											'<span>'+v.mem.name+'</span>'+
											'<span class="mui-h6 mui-pull-right">'+v.addtime+'</span>'+
										'</li>'+
										'<li class="mui-h6 ">'+
											'<span class="iconfont type-3">&#xe626;</span>'+
											'<span>图文聊天&nbsp;</span>'+
										'</li>'+
									'</div>'+
								'</div>'+
							'</div>';
				});
				$.each(data.tell, function(k,v) {
//					console.log(JSON.stringify(v));
					list += '<div class="mui-table-view table-view-cell bbs-ico margin-bottom-5">'+
								'<div class="overflow-h">'+
									'<i><img src="'+getAvatar(v.mem.avatar)+'"/></i>'+
									'<div>'+
										'<li>'+
											'<span>'+v.mem.name+'</span>'+
											'<span class="mui-h6 mui-pull-right">'+v.addtime+'</span>'+
										'</li>'+
										'<li class="mui-h6 ">'+
											'<span class="iconfont type-3">&#xe645;</span>'+
											'<span>电话咨询&nbsp;</span>'+
										'</li>'+
									'</div>'+
								'</div>'+
							'</div>';
				});
				$('.askList').append(list);
				//点击图文聊天
				$('.testPic').unbind('click');
				$('.testPic').click(function(){
					var did = $(this).attr('did');
					goNewPage('home-chat.html',{doctorid:did})
				})
				//点击视频聊天
				$('.testvideo').unbind('click');
				$('.testvideo').click(function(){
					var aid = $(this).attr('aid');//问诊id
					var status = $(this).attr('status');//问诊状态
					var doctorid = $(this).attr('did');//医生id
//					console.log(aid+" "+doctorid+" "+name+" "+avatar);return;
					if(status != '9' && status != '1'){		//可重新发起视频
						console.log(apiRoot+"/home/chat/lineUp/uid/"+uid+"/did/"+doctorid+"/wid/"+aid+"/type/1");
						$.ajax({
							type:"get",
							url:apiRoot+"/home/chat/lineUp",
							data:{
								uid :uid,
								did :doctorid,
								wid :aid,
								type :'1'
							},
							success :function(data){
//								mui.toast('前面有 '+data+' 人排队'); 
								if(data > 0){	//有人排队
									toast('医生忙碌，请稍候重试');
								}else{	//发起视频
									aid = aid.toString();
//									mui.toast('发起视频,问诊id:'+aid+" "+doctorid+" "+name+" "+avatar);
									window.plus.bridge.exec("xiangyu","sendVideo",[doctorid+'',name,avatar, aid]);
									ws.close();
								}
							},
							error :function(e){
								console.log(JSON.stringify(e));
							}
						});
					}
				})
			}
			
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
},false)